package org.example.service;

import org.example.dao.AirDao;
import org.example.entity.Air;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class AirService implements IAirService {
    @Autowired
    private AirDao airDao;

    @Override
    public Long add(Air air) {
        return airDao.save(air).getId();
    }

    @Override
    public int del(Long id) {
        airDao.deleteById(id);
        return 1;
    }

    @Override
    public Long update(Air air) {
        return airDao.save(air).getId();
    }

    @Override
    public Air get(Long id) {
        return airDao.findById(id).get();
    }

    @Override
    public Page<Air> getAll(Air air, PageRequest pageRequest) {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("monitoringStation", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<Air> of = Example.of(air,matcher);
        return airDao.findAll(of, pageRequest);
    }
}
